/**
 * This file is Copyright © 2008 Software Craftsmen Limited. All Rights Reserved.
 */
package com.softwarecraftsmen.generators;

import com.softwarecraftsmen.functors.UnaryFunctor;
import com.softwarecraftsmen.functors.Optional;
import org.jetbrains.annotations.NotNull;

public final class IdentityGenerator<V> extends AbstractGenerator<V>
{
	private final Generator<V> generator;
	private final UnaryFunctor<V> eachFunctor;

	public IdentityGenerator(final @NotNull Generator<V> generator, final @NotNull UnaryFunctor<V> eachFunctor)
	{
		this.generator = generator;
		this.eachFunctor = eachFunctor;
	}

	@NotNull
	public Optional<V> next()
	{
		final Optional<V> nextValue = generator.next();
		if (nextValue.hasSomething().isTrue())
		{
			eachFunctor.closure(nextValue.value());
		}
		return nextValue;
	}
}
